<?php
/*
This file 'pps_base.php' is part of the PPS project <http://code.google.com/p/fracsnetpps/>

Copyright: (C) 2009 Arthur, B. aka ]{ing <whinemore@gmail.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 .
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.
 .
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 .

*/

require ('pps_player_cache.php');
require ('pps_teams.php');

class base_stats{
	
	var $ps; /* Players */
	public $T; /* Teams */
	var $pc; /* Player count */
	var $db;
	var $sock;
	var $buffer;
	var $bots;
	private $glbl_team_cap_modifier;
	private $server_tier;
	private $stmt_player;
	private $player_index; /* By name index of players */
    
    /* 'Cache' */
	private $pcache;
    
/* ---------------------------------------------------------------------------------------------------------------------- */
	public function __construct( &$database, &$socket, &$buffer,  $refresh = null ){
/* ---------------------------------------------------------------------------------------------------------------------- */	
        $this->server_tier = 1;

        /* Varriable Refresh array could be used to set stats */
        /* automatically upon creation of the array			  */
        if( isset( $database ) && isset( $socket )  && isset( $buffer ) ){

            $this->bots = false;
            $this->db =& $database;
            $this->sock =& $socket;
            $this->buffer =& $buffer;

            /* Player Cache */
            $this->pcache = new player_cache();

            /* Teams */
            $this->T = new teams_container($this->sock);

            $this->stmt_player = $this->db->stmt_init();
            $prepare_str  = "UPDATE players SET kills=?,";
            $prepare_str .= "deaths=?,";
            $prepare_str .= "doubles=?,";
            $prepare_str .= "triples=?,";
            $prepare_str .= "multi=?,";
            $prepare_str .= "caps=?,";
            $prepare_str .= "grabs=?,";
            $prepare_str .= "returns=?,";
            $prepare_str .= "kd=?,";
            $prepare_str .= "cg=?,";
            $prepare_str .= "ck=?,";
            $prepare_str .= "rating=?,";
            $prepare_str .= "wins=?,";
            $prepare_str .= "maps=?,";
            $prepare_str .= "time_played=?,";
            $prepare_str .= "lastplayed=NOW(),";
            $prepare_str .= "`Desert Eagles`=`Desert Eagles`+?,";
            $prepare_str .= "`HK MP5`=`HK MP5`+?,";
            $prepare_str .= "`Ak-74`=`Ak-74`+?,";
            $prepare_str .= "`Steyr AUG`=`Steyr AUG`+?,";
            $prepare_str .= "`Spas-12`=`Spas-12`+?,";
            $prepare_str .= "`Ruger 77`=`Ruger 77`+?,";
            $prepare_str .= "`M79`=`M79`+?,";
            $prepare_str .= "`Barrett M82A1`=`Barrett M82A1`+?";
            $prepare_str .= "`FN Minimi`=`FN Minimi`+?,";
            $prepare_str .= "`Selfkill`=`Selfkill`+?,";
            $prepare_str .= "`Combat Knife`=`Combat Knife`+?,";
            $prepare_str .= "`Chainsaw`=`Chainsaw`+?,";
            $prepare_str .= "`LAW`=`LAW`+?,";
            $prepare_str .= "`Grenade`=`Grenade`+?,";
            $prepare_str .= "streak=?,";
            $prepare_str .= "plusminus=?,";				
            $prepare_str .= "mu=?,";
            $prepare_str .= "sigma=?,";
            $prepare_str .= "dominations=?";
            $prepare_str .= " WHERE ip=?";

            $this->stmt_player->prepare($prepare_str) or die("COULD NOT PREP");

            $this->stmt_player->execute() or die("COULD NOT EXECUTE");

            if( isset( $refresh ) ){
                if( $refresh['players'] ){
        
                    $dublicate = false;
        
                    for( $i = 0; $i < $refresh['players']; $i++ ){                        
                        $name = $refresh['player'][$i]['name'];
                        $id = $refresh['player'][$i]['id'];
                        $team = $refresh['player'][$i]['team'];
                        $player = null;

                        $data = mysqli_query($database, "SELECT * FROM players WHERE ip=\"" . $refresh['player'][$i]['ip'] . "\"");

                        $record = mysqli_fetch_array($data);
                        if( $record ) $player = new base_player( $record, $name ); else $player = new base_player( null, $name );

                        $this->T->add( $name, $team, $id, $player );
                                                         
                        /* Add to the cache */
                        if( $record ) $this->pcache->raise( $name, $record );
                        
                     	$this->pc++;
                    }
                }
            }	
        }
    }
/* ++++ Ranking functions ++++ */
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */	
    private function v($val, $e = 0){ /* 'v' function a simple knock off the one shown in TrueSkill(tm) documentation
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
        if( $val > 0 )
            return (pow(1.2, -($val) * 6));
        else
            return (pow(2, -($val) * 0.25));
    }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
    function w($val, $e = 0){ /* 'w' function knock off */
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
        if( $val == 0 )
            return 0.8;
        if( $val < 0 )
            return -( 1 / pow(1.5, -$val) );    
        
        return ( 1 / pow(1.5, $val * 2) );
    }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
    function c_squared($sigma_w, $sigma_l){ /* Return the value of c squared */
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
        return 2 + ($sigma_w * $sigma_w) + ($sigma_l * $sigma_l);
    }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
    function newSigma($sigma, $c, $Mu_w, $Mu_l){ /* Get the new sigma for a player */
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
        return sqrt(($sigma * $sigma) * (1 - ((($sigma*$sigma)/$c) * $this->w( ($Mu_w - $Mu_l)/sqrt($c) )) ));
    }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
    function newMu_w($sigma, $c, $Mu_w, $Mu_l){ /* Get the new Mu for a player(Winner) */
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
        return $Mu_w + ( (($sigma*$sigma)/sqrt($c)) * $this->v( ($Mu_w - $Mu_l)/sqrt($c) ) );
    }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
    function newMu_l($sigma, $c, $Mu_w, $Mu_l){ /* Get the new Mu for a player(Looser) */
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
        return $Mu_l - ( (($sigma*$sigma)/sqrt($c)) * $this->v( ($Mu_w - $Mu_l)/sqrt($c) ) );
    }
    
    
    
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */	
	private function get_refresh(){
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */	
        fputs($this->sock, "REFRESH\r\n");
        
        while( $this->buffer != "REFRESH" ){
            $this->buffer = trim(fgets($this->sock, 1024));
            if( !$this->buffer ) continue;
            else if($this->buffer == "REFRESH") break;
        }

        return GetSoldatInfo($this->sock);
    }
    
    public function grant_acess($ip){
        $data = mysqli_query($this->db, "SELECT user_id FROM players WHERE ip=\"".$ip."\"");
        $result = mysqli_fetch_array($data);
        if( $result ){
            fputs($this->sock,"/password\r\n");
        }
    }
/* ---------------------------------------------------------------------------------------------------------------------- */
	public function ch_join($ip){
/* ---------------------------------------------------------------------------------------------------------------------- */
        $name = substr( $this->buffer ,strpos($this->buffer , "]") + 1);
        $team = (int)substr($this->buffer, strpos($this->buffer, "[") + 1, strpos($this->buffer, "]"));
        $id = (int)substr($this->buffer, 6, strpos($this->buffer, ")"));
        //Debug echo $name, " joined team #",$team," with id #",$id , " and ip: $ip\n";

        $player = new base_player( null, $name );

        if( !array_key_exists($name, $this->T->ps) )
        {   /* Add a new player */
            $record = null;
            /* Before querying the database check the cache */
            if( $ip && $this->pcache->raise( $ip ) ){
                $record = $this->pcache->raise( $ip );
            }else{
                $data = mysqli_query($this->db, "SELECT * FROM players WHERE ip=\"". $ip . "\"");
                if( $data ) $record = mysqli_fetch_array($data);
            }

            if( $record ) $player = new base_player( $record, $name );
        }
        $player->ip = $ip;
        $this->T->add( $name, $team, $id, $player );

        if( $this->T->ps[$name]->is_rated == false && $this->T->ps[$name]->team->number != 5){
            fputs($this->sock, "/setteam5 ". $this->T->ps[$name]->p_id . "\r\n");
            if( $ip ){
                fputs($this->sock, "/pm ". $this->T->ps[$name]->p_id . " NOTICE: ONLY FRACS MEMBERS ARE ALLOWED TO PLAY!\r\n");
                fputs($this->sock, "/pm ". $this->T->ps[$name]->p_id . " Becoming a member will allow us to rank you and keep your stats.\r\n");
                fputs($this->sock, "/pm ". $this->T->ps[$name]->p_id . " Registering is free and simple at http://fracs.net/pps/\r\n");
            }
        }
        return $ret;
	}
/* ------------------------------------------------------------------------------------------------------------------- */
    public function ch_left()
/* ------------------------------------------------------------------------------------------------------------------- */
    {
        $name = substr( $this->buffer ,strpos($this->buffer , "]") + 1);
        $team = (int)substr($this->buffer, strpos($this->buffer, "[") + 1, strpos($this->buffer, "]"));
        $id = (int)substr($this->buffer, 6, strpos($this->buffer, ")"));
        //Debug echo $name, " left team #", $team, " with id #", $id, "\n";
        
        if( $this->T->ps[$name]->is_rated ){
            if( (time() - $this->T->ps[$name]->map_timer) > 299 )
                $this->T->ps[$name]->maps++;
            
            $this->update_player_stats($name);
            
        }

        $p_id = $this->T->ps[$name]->p_id;

        foreach( $this->T->ps as $key => $plr )
            $this->T->ps[$key]->dominated[$p_id] = 0;
    
        $this->T->remove($name);
    }

/* ---------------------------------------------------------------------------------------------------------------------- */
	/* Returns false on ahem, false positive.. */
    public function ch_kill(/*void*/){
/* ---------------------------------------------------------------------------------------------------------------------- */
        $loc = (strpos($this->buffer, "with")) + 5;
        $weapon = substr($this->buffer, $loc);
        $kn = substr($this->buffer, 4, strpos($this->buffer, " killed") - 4);
        $vn = substr($this->buffer, strpos($this->buffer, " killed (") + 12, strpos($this->buffer, " with") - (strpos($this->buffer, " killed (") + 12));
        
        //echo $kn , " --- ", $vn , "\n";
        if( !array_key_exists($kn, $this->T->ps) || !array_key_exists($vn, $this->T->ps) ){
            echo "Error killer/victim is not present in system!\n";
            return;
        }
        
        $killer = &$this->T->ps[$kn];
        $victim = &$this->T->ps[$vn];
        
        $killer->update_kills($weapon);
        $victim->d++;
        
        if( $killer->is_rated ){
            $kill = time();
            $speed = ( $killer->kill_count > 1 ) ? ($killer->kill_count + 1.02) : (2.02);
            if( $kill - $killer->kill_timer < $speed ){
                $killer->kill_count++;
            }else{
                $killer->mlt[$killer->kill_count]++;
                $killer->kill_count = 0;
            }
            
            $killer->kill_timer = $kill;
        }
        
        $killer->dominated[$victim->p_id]++;
        $victim->dominated[$killer->p_id] = 0;
        //echo $killer->dominated[$victim->p_id], "/", $victim->dominated[$killer->p_id], "\n";
        if( $killer->dominated[$victim->p_id] > 3 ){
            $k = ( $killer->acc_name ) ? $killer->acc_name : $kn;
            $d = ( $victim->acc_name ) ? $victim->acc_name : $vn;
            switch( $killer->dominated[$victim->p_id] )
            {
                case 4:
                    $domn = "/say $k is harrassing $d...\r\n";
                    fputs($this->sock, $domn);
                    $killer->dominations++;
                    break;
                case 6:
                    $domn = "/say $k is destroying $d!\r\n";
                    fputs($this->sock, $domn);
                    $killer->dominations++;
                    break;
                case 8:
                    $domn = "/say $k is DOMINATING $d!!\r\n";
                    fputs($this->sock, $domn);
                    $killer->dominations++;
                    break;
                case 10:
                    $domn = "/say $k is beating $d like he owes him money..!\r\n";
                    fputs($this->sock, $domn);
                    $killer->dominations++;
                    break;
                case 20:
                    $domn = "/say Holly shit $k can you leave $d alone?\r\n";
                    fputs($this->sock, $domn);
                    $domn = "/say I mean really, what has $d ever done to deserve this.\r\n";
                    fputs($this->sock, $domn);
                    $domn = "/say Youre going to make him quit this game.....\r\n";
                    fputs($this->sock, $domn);
                    $killer->dominations++;
                    break;
            }
       }

    }
/* ---------------------------------------------------------------------------------------------------------------------- */
    public function ch_grab(/*void*/){
/* ---------------------------------------------------------------------------------------------------------------------- */
        $grab = substr($this->buffer, 0, strpos($this->buffer, "captured the") - 1);
        $this->T->ps[$grab]->g++;
    }

/* ---------------------------------------------------------------------------------------------------------------------- */
    public function ch_cap(/*void*/){
/* ---------------------------------------------------------------------------------------------------------------------- */
		$name = substr($this->buffer, 0, strpos($this->buffer, "scores for") - 1);
        $this->T->ps[$name]->team->score++; 
        $cteam = $this->T->ps[$name]->team->number;
        echo "$name capped on team $cteam with score of ", $this->T->ps[$name]->team->score , "\n";
        foreach( $this->T->ps as $key => $player ){
            if( $this->T->ps[$key]->team->number == $cteam )
                $this->T->ps[$key]->plus_minus++;
            else
                $this->T->ps[$key]->plus_minus--;
        }
                
        $this->T->ps[$name]->c++;
    }
/* ---------------------------------------------------------------------------------------------------------------------- */
    public function ch_return(/*void*/){
/* ---------------------------------------------------------------------------------------------------------------------- */
		$name = substr($this->buffer, 0, strpos($this->buffer, "returned the") - 1);
        $this->T->ps[$name]->r++;
    }
/* ---------------------------------------------------------------------------------------------------------------------- */
    public function ch_nextmap(/*void*/){
/* ---------------------------------------------------------------------------------------------------------------------- */
		
        if( !$this->T->pc ) return;
        $winner = 0;
		if( $this->T->alpha->score != $this->T->bravo->score )
		{
			if( $this->T->alpha->score > $this->T->bravo->score ){
				$winner = 1;
                $rwin = &$this->T->alpha;
                $rlos = &$this->T->bravo;
            }
			else{
				$winner = 2;
                $rwin = &$this->T->bravo;
                $rlos = &$this->T->alpha;
            }
		}
		// Update Rankings ( WIP )
        $Sw = 0; // sigma winner
        $Sl = 0; // sigma looser 
        $Mw = 0; // Mu winner
        $Ml = 0; // Mu looser
        $c = $this->c_squared($rwin->sigma, $rlos->sigma);

        if( $rwin->count && $rlos->count){
        //WINNING PLAYERS
        foreach( $rwin->p as $key => $player ){
            if( !$this->T->ps[$player]->is_rated ) continue;
            
            $sigma = $this->T->ps[$player]->sigma;
            $mu = $this->T->ps[$player]->mu;
            $c = $this->c_squared( $sigma, ($rlos->sigma/$rlos->count) );
            
            //new sigma
            $this->T->ps[$player]->sigma = $this->newSigma( $sigma, $c, $mu, ($rlos->mu/$rlos->count) );
            //new mu
            $this->T->ps[$player]->mu = $this->newMu_w( $sigma, $c, $mu, ($rlos->mu/$rlos->count) );
            //new rating
            $this->T->ps[$player]->rating = $this->T->ps[$player]->mu - ($this->T->ps[$player]->sigma * 2.5);
            
            $Sw += $this->T->ps[$player]->sigma;
            $Mw += $this->T->ps[$player]->mu;
            
            if( time() - $this->T->ps[$player]->map_timer > 299 ){
                $this->T->ps[$player]->maps++;
                $this->T->ps[$player]->wins++;
            }
            
            $this->ps[$player]->map_timer = time();
            
            $this->update_player_stats($player);
        }
        }
        
        if( $rlos->count && $rwin->count ){
        //LOOSING PLAYERS
        foreach( $rlos->p as $key => $player ){
            if( !$this->T->ps[$player]->is_rated ) continue;

            $sigma = $this->T->ps[$player]->sigma;
            $mu = $this->T->ps[$player]->mu;
            $c = $this->c_squared( ($rwin->sigma/$rwin->count), $sigma );

            
            $this->T->ps[$player]->sigma = $this->newSigma( $sigma, $c, ($rwin->mu/$rwin->count), $mu );
            
            $this->T->ps[$player]->mu = $this->newMu_l( $sigma, $c, ($rwin->mu/$rwin->count), $mu );
            
            $this->T->ps[$player]->rating = $this->T->ps[$player]->mu - ($this->T->ps[$player]->sigma * 2.5);
            
            $Sl += $this->T->ps[$player]->sigma;
            $Ml += $this->T->ps[$player]->mu;
            
            if( time() - $this->T->ps[$player]->map_timer > 299 ){
                $this->T->ps[$player]->maps++;
            }
            
            $this->ps[$player]->map_timer = time();

            $this->update_player_stats($player);
        }    
      
        }
       
        if( $Mw ){
            $rwin->mu = $Mw;
            $rwin->sigma = $Sw;
        }
        if( $Ml ){
            $rlos->mu = $Ml;
            $rlos->sigma = $Sw;
        }
        $this->T->alpha->score = 0;
        $this->T->bravo->score = 0;
       
        foreach( $this->T->ps as $key => $player ){
            for( $i = 0; $i < 32; $i++ ){
                $this->T->ps[$key]->dominated[$i] = 0;
            }
        }
        
        $this->T->shuffle();
	}
/* ---------------------------------------------------------------------------------------------------------------------- */
	public function update_player_stats($p_num)
/* ---------------------------------------------------------------------------------------------------------------------- */
	{
		/* First Part Update Player specific stats */
        $P = &$this->T->ps[$p_num];
		$P->update_kdr();
		$P->update_cgr();
		$P->update_ckr();
		$P->update_time_played();
		
		$this->stmt_player->bind_param('iiiiiiiiddddiiiiiiiiiiiiiiiiiiiidds', 
                                        $P->k,
                                        $P->d,
                                        $P->mlt[1],
                                        $P->mlt[2],
                                        $P->mlt[3],
                                        $P->c,
                                        $P->g,
                                        $P->r,
                                        $P->kdr,
                                        $P->cgr,
                                        $P->ckr,
                                        $P->rating,
                                        $P->wins,
                                        $P->maps,
                                        $P->tp,
                                        $P->w['Desert Eagles'], 
                                        $P->w['HK MP5'], 
                                        $P->w['Ak-74'],
                                        $P->w['Steyr AUG'],
                                        $P->w['Spas-12'],
                                        $P->w['Ruger 77'],
                                        $P->w['M79'],
                                        $P->w['Barrett M82A1'],
                                        $P->w['FN Minimi'],
                                        $P->w['XM214 Minigun'],
                                        $P->w['Selfkill'],
                                        $P->w['Combat Knife'],
                                        $P->w['Chainsaw'],
                                        $P->w['LAW'],
                                        $P->w['Grenade'],
                                        $P->wstreak,
                                        $P->plus_minus,
                                        $P->mu,
                                        $P->sigma,
                                        $P->ip);
																
		$this->stmt_player->execute() or die("Could Not Execute Prepared Statement\n");
						
		foreach ($P->w as $key => $weapon )
		{
			if( $weapon )
			{
				$query = "UPDATE weapons SET kills=kills+" . $weapon . " WHERE weapon = \"" . $key . "\"";
				mysqli_query($this->db, $query);
			}
			$P->w[$key] = 0;
		}
        
        /* Update the cache */
        $record = array();
        $record['name'] = $P->acc_name;
        $record['kills'] = $P->k;
        $record['deaths'] = $P->d;
        $record['caps'] = $P->c;
        $record['grabs'] = $P->g;
        $record['returns'] = $P->r;
        $record['tier'] = $P->t;
        $record['maps'] = $P->maps;
        $record['wins'] = $P->wins;
        $record['kd'] = $P->kdr;
        $record['cg'] = $P->cgr;
        $record['time_played'] = $P->tp;
        $record['points'] = $P->points;
        $record['ip'] = $P->ip;
        $record['dobules'] = $P->mlt[1];
        $record['triples'] = $P->mlt[2];
        $record['multi'] = $P->mlt[3];
        $record['life_span'] = $P->alive_time;
        $record['streak'] = $P->wstreak;
        $record['plusminus'] = $P->plus_minus;
        $record['mu'] = $P->mu;
        $record['sigma'] = $P->sigma;
        $record['rating'] = $P->rating;
        
        $this->pcache->raise( $p_num->ip, $record );
	}
/* ---------------------------------------------------------------------------------------------------------------------- */
	public function hook_accuracy()
/* ---------------------------------------------------------------------------------------------------------------------- */
	{
		/* WIP */
		/* All of this bellow this line is just pulling information from the encoded line printed by the accuracy soldat script */
		$u_ip = substr($this->buffer, 5, strpos($this->buffer, '[/acc]') - 5 );
		/* Check if the player is actually rated(NOTE: this should not be needed in any code later,
           since only registered players can play in the first place */
        $query = "SELECT * FROM accuracy WHERE ip='$u_ip'";
        $data = mysqli_query($this->db, $query);
		if( !$data ) return;
        $plr = mysqli_fetch_array($data);
        
        /* Uhg this part is a bit ugly but oh well... */
		$info_end = strpos($this->buffer, '[/acc]') + 7;
		$us_dmg = substr($this->buffer, $info_end, strpos($this->buffer, 's',$info_end) - $info_end);
		$info_end += strlen($us_dmg) + 1;
		$us_shots = substr($this->buffer, $info_end, strpos($this->buffer, 'h', $info_end) - $info_end);
		$info_end += strlen($us_shots) + 1;
		$us_hits = substr($this->buffer, $info_end, strpos($this->buffer, 'w', $info_end) - $info_end);
		$info_end += strlen($us_hits) + 4;
		
        
        /* I changed this so that the update code now only uses 2 string arrays, instead of 4(2 strings/2 ints) */
        /* Should save time on typecasting now */
		$us_whit = array(); /* string for hits */
		$us_wshot = array(); /* string for shots */
        $cur_acc = 0;
        
		$us_whit[0] = "0";
		$us_wshot[0] = "0";
            
        /* weapon_number == 0 is Knife, since we dont rate knifes, we start at 1 */
		for( $k = 1; $k < 11; $k++ )
		{
            /* Okay i know this is cumbersome but its better than the old way(**faster**) */
            $l = $k + 1;
            $us_wshot[$k] = substr($this->buffer, $info_end, strpos($this->buffer, ':', $info_end) - $info_end);
			$info_end += strlen($us_wshot[$k]) + 1;
			$us_whit[$k] = substr($this->buffer, $info_end, strpos($this->buffer, "[$l]", $info_end) - $info_end);
			$info_end += strlen($us_whit[$k]) + 3;
            
            if( $us_wshot[$k] != "0" )
            {
                // 1+$k is the start of the weapons: [2]Desert Eagles,[3]Hk[4]Ak ...              
                /* If the player has 0% accuracy just give him w/e he got this game */
                if( $us_whit[$k] != "0" )
                {
                    $cur_acc = round( ($us_whit[$k]/$us_wshot[$k]), 2 );
                    if( $plr[1 + $k] )
                        $plr[1 + $k] = round( ($plr[1+$k] + $cur_acc)/2, 2 );
                    else
                        $plr[1 + $k] = $cur_acc;
                }else
                    $plr[1 + $k] = round( $plr[1 + $k]/2, 2 );   
            }
        }
				
		$query = "UPDATE accuracy SET `Desert Eagles`=". 		$plr[2] . ",".
											"`HK MP5`=". 		$plr[3] . ",".  
											"`Ak-74`=". 		$plr[4] . ",". 
											"`Steyr AUG`=". 	$plr[5] . ",".
											"`Ruger 77`=". 		$plr[7] . ",".
											"`M79`=". 			$plr[8] . ",".
											"`Barrett M82A1`=". $plr[9] . ",".
											"`FN Minimi`=". 	$plr[10] . ",". 
											"`damage`=`damage`+$us_dmg,". 
											"`shots`=`shots`+$us_shots,".
											"`hits`=`hits`+$us_hits". " WHERE `ip`=\"$u_ip\"";
		mysqli_query($this->db, $query);
	
	}
}
